{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn import datasets\n",
    "from sklearn import cross_validation\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 鸢尾花分类的数据 采用分层采样\n",
    "def load_data():\n",
    "    iris = datasets.load_iris()\n",
    "    return cross_validation.train_test_split(iris.data,iris.target,test_size=0.25,random_state=0, stratify=iris.target)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 测试分类决策树函数：\n",
    "def test_DecisionTreeClassifier(*data):\n",
    "    from sklearn.tree import export_graphviz\n",
    "    \n",
    "    X_train,X_test,Y_train,Y_test = data\n",
    "    regr = DecisionTreeClassifier()\n",
    "    regr.fit(X_train,Y_train)\n",
    "    # 本地保存graphviz文件\n",
    "    export_graphviz(regr,'3-3.out')\n",
    "    \n",
    "    print('【训练集成绩】Training Score:{0}'.format(regr.score(X_train,Y_train)))\n",
    "    print('【测试集成绩】Testing  Score:{0}'.format(regr.score(X_test,Y_test)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "【训练集成绩】Training Score:1.0\n",
      "【测试集成绩】Testing  Score:0.9473684210526315\n"
     ]
    }
   ],
   "source": [
    "# 开始测试：\n",
    "X_train,X_test,Y_train,Y_test = load_data()\n",
    "test_DecisionTreeClassifier(X_train,X_test,Y_train,Y_test)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 生成图片的方法：\n",
    "### 在3-3out文件目录下：> dot 3-3.out -T pdf -o out.pdf "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
